﻿/*
Copyright (C) 2008-2011 ForceMem (Siarhei Fedarenko)
GNU General Public License version 3 (GPLv3)
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace TranscriptionWriter
{
    public class SymbolKey : IComparable, IEnumerable
    {
        public SymbolKey(string key)
        {
            if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key");
            m_Key = key;
        }

        string m_Key = null;
        public string Key { get { return m_Key; } }

        Rule m_DefaultRule = null;
        public Rule DefaultRule { get { return m_DefaultRule; } }

        ArrayList rules = new ArrayList();

        public void AddRule(Rule rule)
        {
            foreach (Rule r in this.rules)
            {
                if (rule.Transcript == r.Transcript && rule.Example == r.Example)
                    return;
            }
            rules.Add(rule);
            if (rule.Transcript == this.Key)
                this.m_DefaultRule = rule;
        }

        public override string ToString()
        {
            return m_Key;
        }

        // Returns:
        //     A 32-bit signed integer that indicates the relative order of the objects
        //     being compared. The return value has these meanings: Value Meaning Less than
        //     zero This instance is less than obj. Zero This instance is equal to obj.
        //     Greater than zero This instance is greater than obj.
        public int CompareTo(object obj)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            return this.Key.CompareTo(((SymbolKey)obj).Key);
        }

        public IEnumerator GetEnumerator()
        {
            return this.rules.GetEnumerator();
        }
    }

    public class Rule
    {
        public Rule(string transcript, string ruleDescr, string example)
        {
            this.m_Transcript = transcript;
            this.m_RuleDescr = ruleDescr;
            this.m_Example = example;
        }

        string m_Transcript = null;
        public string Transcript { get { return m_Transcript; } }

        string m_RuleDescr = null;
        public string RuleDescr { get{ return m_RuleDescr; } }

        string m_Example = null;
        public string Example { get{ return m_Example; } }

        public override string ToString()
        {
            string ret = "";
            ret = "/" + this.Transcript + "/";
            if (!string.IsNullOrEmpty(this.RuleDescr))
                ret += " " + this.RuleDescr;
            if (!string.IsNullOrEmpty(this.Example))
                ret += "  - \"" + this.Example + "\"";
            return ret;
        }
    }
}
